package org.pf.semaphore 
{
	import org.pf.base.pfBlocks.PFSemaphore;
	import org.pf.semaphore.events.PFCommonSemaphoreEvent;
	
	/**
	 * ...
	 * @author 
	 */
	public class PFCommonSemaphore extends PFSemaphore 
	{
		protected var _locks:int = 0;
		private var _inProcess:Boolean = false;
		public function PFCommonSemaphore(owner:Object = null, description:String = null) 
		{
			super(owner, description);
		}
		
		override protected function initialize():void
		{
			_locks = 0;
			_inProcess = false;
		}
		
		override public function get locks():int
		{
			return _locks;
		}
		
		override public function get isLocked():Boolean
		{
			return (_locks > 0);
		}
		
		public function get inProcess():Boolean 
		{
			return _inProcess;
		}
		
		public function set inProcess(value:Boolean):void 
		{
			_inProcess = value;
		}
		
		public function lock():void
		{
			if (_locks++ == 0)
			{
				//TODO : внести на семафоры прослушиватели событий в классах апдейтерах
				dispatchEvent(new PFCommonSemaphoreEvent(PFCommonSemaphoreEvent.UNAVAILABLE));
			}
		}
		
		public function unlock():void
		{
			_locks--;
			if (locks < 0)
			{
				throw new Error("Semaphore Asymmetrically Unlocked!");
				locks = 0;
			}
			else if (locks == 0)
			{
				dispatchEvent(new PFCommonSemaphoreEvent(PFCommonSemaphoreEvent.AVAILABLE));
			}
		}
		
	}

}